﻿using WWLauncher.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace WWLauncher.Util
{
    public class FileWatcher
    {
        public static Dictionary<FileSystemWatcher, MenuViewModel> LinkMenuMap = new();

        /// <summary>
        /// 实时文件夹监听
        /// </summary>
        /// <param name="menuInfo"></param>
        public static void LinkMenuWatcher(MenuViewModel menuInfo)
        {
            try
            {
                var fileSystemWatcher = new FileSystemWatcher
                {
                    Path = menuInfo.LinkPath,
                };
                LinkMenuMap.Add(fileSystemWatcher, menuInfo);
                fileSystemWatcher.EnableRaisingEvents = true;
                fileSystemWatcher.Changed += LinkIcon_Changed;
                fileSystemWatcher.Deleted += LinkIcon_Deleted;
                fileSystemWatcher.Created += LinkIcon_Created;
                fileSystemWatcher.Renamed += LinkIcon_Renamed;
            } catch (Exception e)
            {
                LogUtil.WriteErrorLog(e, "添加LinkMenu监听异常");
            }
            
        }

        private static void LinkIcon_Renamed(object sender, RenamedEventArgs e)
        {
            IconInfo iconInfo = GetIconInfoByPath(sender, e.OldFullPath);
            iconInfo.Name = e.Name;
            iconInfo.TargetPath = e.FullPath;
        }

        private static void LinkIcon_Changed(object sender, FileSystemEventArgs e)
        {
            var iconInfo = GetIconInfoByPath(sender, e.FullPath);
            if (iconInfo != null)
            {
                var newIconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
                iconInfo.BitmapImage = newIconInfo.BitmapImage;
            }
            
        }
        private static void LinkIcon_Deleted(object sender, FileSystemEventArgs e)
        {
            var iconInfo = GetIconInfoByPath(sender, e.FullPath);
            App.Current.Dispatcher.Invoke(() =>
            {
                LinkMenuMap[sender as FileSystemWatcher].IconList.Remove(iconInfo);
            });
        }
        private static void LinkIcon_Created(object sender, FileSystemEventArgs e)
        {
            var iconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
            App.Current.Dispatcher.Invoke(() =>
            {
                LinkMenuMap[sender as FileSystemWatcher].IconList.Add(iconInfo.To()) ;
            });
        }

        private static IconInfo GetIconInfoByPath(object sender, string path)
        {
            MenuViewModel menuInfo = LinkMenuMap[sender as FileSystemWatcher];
            foreach (IconInfo iconInfo in menuInfo.IconList)
            {
                if (iconInfo.TargetPath.Equals(path))
                {
                    return iconInfo;
                }
            }
            return null;
        }

        /// <summary>
        /// 开启所有菜单监听
        /// </summary>
        /// <param name="appData"></param>
        public static void EnableLinkMenuWatcher(MainViewModel appData)
        {
            foreach (MenuViewModel menuInfo in appData.MenuList)
            {
                if (menuInfo.MenuType == Constant.MenuType.Link)
                {
                    LinkMenuWatcher(menuInfo);
                }
            }
            RefreshLinkMenuIcon(appData);
        }

        private static void RefreshLinkMenuIcon(MainViewModel appData)
        {
            new Thread(() =>
            {
                try
                {
                    foreach (MenuViewModel menuInfo in appData.MenuList)
                    {
                        if (menuInfo.MenuType == Constant.MenuType.Link)
                        {
                            var dirInfo = new DirectoryInfo(menuInfo.LinkPath);
                            var fileInfos = dirInfo.GetFileSystemInfos();

                            var iconList = new ObservableCollection<IconInfo>();
                            foreach (var fileInfo in fileInfos)
                            {
                                var iconInfo = CommonCode.GetIconInfoByPath_NoWrite(fileInfo.FullName).To();
                                iconList.Add(iconInfo);
                            }
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                //有可能有问题，原来的代码确实如此
                                foreach (IconInfo iconInfo in iconList)
                                {
                                    menuInfo.IconList = null;
                                    menuInfo.IconList = iconList;
                                }
                            });
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            }).Start();
        }

        /// <summary>
        /// 移除菜单监听
        /// </summary>
        /// <param name="menuInfo"></param>
        public static void RemoveLinkMenuWatcher(MenuViewModel menuInfo)
        {
            try
            {
                foreach (FileSystemWatcher watcher in LinkMenuMap.Keys)
                {
                    if (LinkMenuMap[watcher] == menuInfo)
                    {
                        //释放资源
                        watcher.Changed -= LinkIcon_Changed;
                        watcher.Created -= LinkIcon_Created;
                        watcher.Deleted -= LinkIcon_Deleted;
                        watcher.Renamed -= LinkIcon_Renamed;
                        watcher.EnableRaisingEvents = false;
                        watcher.Dispose();
                        LinkMenuMap.Remove(watcher);
                    }
                }
            }
            catch 
            {
                //nothing
            }
        }






      
    }
}
